#-*- coding: utf-8 -*-
'''
Created on Aug 22, 2012

@author: bbg
'''
import sys
import os
from sqlalchemy import Integer, Column, String, create_engine, MetaData, Table
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, mapper, clear_mappers
from sqlalchemy.pool import Pool
from sqlalchemy.pool import NullPool
from common.debug import Debug
 

class DB_Instance:

    def __init__(self):
        #self.wugangpool=Pool()
        #self.wugangpool._recycle=20  
        #self.debug=Debug()      
        self.MySqlEngine =create_engine(
                                        "mysql://apkpos:apkpos@58.215.189.116:3306/company_management",
                                        poolclass=NullPool,
                                        connect_args={"charset":"utf8"})  
        self.Session = sessionmaker()  
        
        '''
        c1 = self.MySqlEngine.connect()  
        c2 = self.MySqlEngine.connect()  
        c3 = self.MySqlEngine.connect()  
 
        c1.close()  
        c2.close()  
        c3.close()  
        for i in xrange(5):  
            c = self.MySqlEngine.connect()  
            self.debug.d("fetchall：%s" % c.execute("select 1").fetchall())   
            c.close()  
        '''
        
        self.Session.configure(bind=self.MySqlEngine)  

        pass 
    


    def session(self):
        
        #conn =self.MySqlEngine.connect()
        #return self.Session(bind=conn)
        return self.Session()


#Base = declarative_base()
#
#class UserInfoMapping(object):
#    def __init__(self):
#        self.name = ""
#        self.pwd = ""
#        self.shop_id = 0
#        self.phone = ""
#        self.email = ""
#
#
#    
#MySqlEngine = create_engine('mysql://apkpos:apkpos@58.215.189.116:3306/company_1')
#Session = sessionmaker(bind = MySqlEngine)
#
#metaData = MetaData()
#
#userTable = Table('user', metaData, 
#                      Column('id', Integer, primary_key = True),
#                      Column('name', String),
#                      Column('pwd', String),
#                      Column('shop_id', Integer),
#                      Column('phone', String),
#                      Column('email', String))
#
#Mapper = mapper(UserInfoMapping, userTable)
#
#u = UserInfoMapping()
#u.name = '小二'
#u.pwd = 'wbc'
#u.shopId = 2
#
#session = Session()
#
#session.add(u)
#
#session.commit()
#
##-----second table 
##clear_mappers()
#Mapper.dispose()
#
#userTable = Table('user_1', metaData, 
#                      Column('id', Integer, primary_key = True),
#                      Column('name', String),
#                      Column('pwd', String),
#                      Column('shop_id', Integer),
#                      Column('phone', String),
#                      Column('email', String))
#
#Mapper = mapper(UserInfoMapping, userTable)
#
#u = UserInfoMapping()
#u.name = '小a'
#u.pwd = 'wbc'
#u.shopId = 3
#
#session.add(u)
#
#session.commit()
#
#session.close()
